#include "PreCompiled.h"
#include "CommandHandler.h"
#include "Application_App.h"
#include "Document_App.h"
CommandHandler::CommandHandler(const char* CommandName)
:_commandName(CommandName)
{
}

CommandHandler::~CommandHandler(void)
{
}

////////////////////////////////////////////////////////////////////////

openFileCommand::openFileCommand(const char* commandName)
:CommandHandler(commandName)
{

}

void openFileCommand::excuteCommand(const char* v )
{
    GetApplication().getActiveDocument()->save();
}

////////////////////////////////////////////////////////////////////////

saveFileCommand::saveFileCommand(const char* commandName)
:CommandHandler(commandName)
{

}


void saveFileCommand::excuteCommand(const char* v)
{
	GetApplication().getActiveDocument()->save();
}

setLabelCommand::setLabelCommand(const char* commandName)
:CommandHandler(commandName)
{

}
void setLabelCommand::excuteCommand(const char* v)
{
   GetApplication().getActiveDocument()->setlabelValue(v);
}

setFileNameCommand::setFileNameCommand(const char* commandName)
:CommandHandler(commandName)
{

}
void setFileNameCommand::excuteCommand(const char* v)
{
    GetApplication().getActiveDocument()->setFileValue(v);
}
//////////////////////////////////////////////////////////////////////////
/*                                                                     */
//////////////////////////////////////////////////////////////////////////


CommandHandlerManager* CommandHandlerManager::_instance = 0;

CommandHandlerManager::CommandHandlerManager()
{
   initCommandHandler();
}
CommandHandlerManager::~CommandHandlerManager()
{
   clear();
}
void CommandHandlerManager::initCommandHandler()
{
	CommandHandler* savefile = new saveFileCommand("SaveFileCommand");
	CommandHandler* openfile = new openFileCommand("OpenFileCommand");
	CommandHandler* setfn = new setFileNameCommand("setFileNameCommand");
	CommandHandler* setLabel = new setLabelCommand("setLabelCommand");

	addCommand("SaveFileCommand",savefile);
	addCommand("OpenFileCommand",openfile);

	addCommand("setFileNameCommand",setfn);
	addCommand("setLabelCommand",setLabel);

}
CommandHandlerManager* CommandHandlerManager::getSingleton()
{
	if(_instance == 0)
		_instance = new CommandHandlerManager;
	return _instance;
}

void CommandHandlerManager::excuteCommandByName(const char*commandName,const char* value)
{
	CommandHandler* pHandler = getCommand(commandName);
	pHandler->excuteCommand(value);
}

void CommandHandlerManager::addCommand(const char*CommandName,CommandHandler* handler)
{
    _commandList[CommandName] = handler;
}
void CommandHandlerManager::removeCommand(CommandHandler* handler)
{
	std::map<std::string,CommandHandler*>::iterator it = _commandList.begin();
	for(; it != _commandList.end(); it++)
	{
		if(handler == it->second)
		{
			delete it->second;
			it->second = NULL;
			_commandList.erase(it);	
			break;
		}
	}
}
void CommandHandlerManager::removeCommand(const char*commandName)
{
	std::map<std::string,CommandHandler*>::iterator it = _commandList.find(commandName);
	if(it != _commandList.end())
		_commandList.erase(it);
}
void CommandHandlerManager::clear()
{
	std::map<std::string,CommandHandler*>::iterator it = _commandList.begin();

	for (; it != _commandList.end(); it++)
	{
		delete it->second;
		it->second = NULL;
	}
   _commandList.clear();
}

CommandHandler* CommandHandlerManager::getCommand(const char* name)const
{
	std::map<std::string,CommandHandler*>::const_iterator it = _commandList.find(name);
	return it != _commandList.end() ? it->second : NULL;
}
